package GUI;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JToolBar;

public class WorkSpaceScreen extends JFrame implements ActionListener,ItemListener
{
	private static WorkSpacePanel panel;
	private JLabel statusbar;
	public static JToolBar toolbar;
	private static Icon play,stop,wire,redo,undo;
	private JMenuItem EditItem;
	private Action New = new NewAction();
	private Action Open = new OpenAction();
    private Action Save = new SaveAction();
    private Action Exit = new ExitAction();
    private Action About = new AboutAction();
    private Action Wiki = new WikiAction();
    private Action Updates = new UpdatesAction();
    private JFileChooser _fileChooser = new JFileChooser();
    private static boolean flag=true,WireFlag=false;
    private static JButton PlayButton,StopButton,RedoButton,UndoButton,WireButton,AddGateButton,Customise;
    WorkSpaceScreen()
	{
		super("The Circuit Pro");
		load_image();
		panel = new WorkSpacePanel(10,20);
		statusbar = new JLabel("Welcome to Circuit Pro");
		this.setJMenuBar(CreateMenu());
		add(panel,BorderLayout.CENTER);
		add(CreateToolbar(),BorderLayout.NORTH);
		Customise.addActionListener(this);
		AddGateButton.addActionListener(this);
		PlayButton.addActionListener(this);
		StopButton.addActionListener(this);
		RedoButton.addActionListener(this);
		UndoButton.addActionListener(this);
		WireButton.addActionListener(this);
		
		add(statusbar,BorderLayout.SOUTH);
	}
	public static int height()
	{
		return (panel.getHeight()-toolbar.getHeight());
	}
	public static int width()
	{
		return panel.getWidth();
	}
	private void load_image()
	{
		try
		{
			play = new ImageIcon( getClass().getResource("Play.png"));
			stop = new ImageIcon( getClass().getResource("Stop.png"));
			redo = new ImageIcon( getClass().getResource("reundo.png"));
			undo = new ImageIcon( getClass().getResource("undo.png"));
			wire = new ImageIcon( getClass().getResource("Wire.png"));			
		}
		catch(Exception e)
		{
			JOptionPane.showMessageDialog(null, "An error occured");
		}
	}
	
	public static JToolBar CreateToolbar()
	{
		toolbar = new JToolBar();
		//toolbar.setBackground(new Color(210,210,210));
		PlayButton = new JButton("",play);
		StopButton = new JButton("",stop);
		RedoButton = new JButton("",redo);
		UndoButton = new JButton("",undo);
	    WireButton = new JButton("",wire);
	    AddGateButton = new JButton("Add Gate");
		Customise = new JButton("Customise");
		
		PlayButton.setBorderPainted(false);
		PlayButton.setToolTipText("Play Button");
				
		StopButton.setBorderPainted(false);		
		StopButton.setToolTipText("Stop Button");
				
	    RedoButton.setBorderPainted(false);
	    RedoButton.setToolTipText("Redo Button");	   
	    
	    UndoButton.setBorderPainted(false);		
	    UndoButton.setToolTipText("Undo Button");	    
	    
	    WireButton.setBorderPainted(false);		
	    WireButton.setToolTipText("Wire Button");
	    WireButton.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent event)
			{				
				WireFlag=true;
			}
	    	
	    });
	   	    
	    AddGateButton.setBorderPainted(false);
	    AddGateButton.setToolTipText("Add Gate Button");	   
	    
	    Customise.setBorderPainted(false);
		Customise.setToolTipText("Customise The toolbar");
		if(flag==true)
		{
			toolbar.add(PlayButton);
			toolbar.add(StopButton);
			toolbar.addSeparator();
			toolbar.add(UndoButton);
			toolbar.add(RedoButton);
			toolbar.addSeparator();
			toolbar.add(WireButton);
			toolbar.add(AddGateButton);
			toolbar.addSeparator();
			toolbar.add(Customise);
		}
		else if(flag==false)
		{
			//for
		}
		return toolbar;
	}
	
	public static boolean getWireFlag()
	{
		return WireFlag;
	}
	public static boolean toggleWireFlag()
	{
		if(WireFlag==true)
		{
			WireFlag=false;
			return WireFlag;
		}
		else
		{
			WireFlag=true;
			return WireFlag;
		}
	}
	protected JMenuBar CreateMenu()
	{

		JMenuBar fileBar = new JMenuBar();
		//fileBar.setBackground(Color.DARK_GRAY);
		
		JMenu file = new JMenu("File");
	    JMenu edit = new JMenu("Edit");
	    JMenu help= new JMenu("Help");
	    
	    //file.setBackground(Color.DARK_GRAY);
		file.setMnemonic(KeyEvent.VK_F);
		
		//edit.setBackground(Color.DARK_GRAY);
		edit.setMnemonic(KeyEvent.VK_E);
		
		help.setMnemonic(KeyEvent.VK_H);
		//help.setBackground(Color.DARK_GRAY);
		
		fileBar.add(file);
		fileBar.add(edit);
		fileBar.add(help);
		
		file.add(New);
		file.addSeparator();
		file.add(Open);
		file.add(Save);
		file.addSeparator();
		file.add(Exit);
		
		EditItem = new JMenuItem("Undo",undo);
		EditItem.addItemListener(this);
		edit.add(EditItem);
		EditItem = new JMenuItem("Redo",redo);
		edit.add(EditItem);
		edit.addSeparator();
		EditItem = new JMenuItem("Delete");
		edit.add(EditItem);
		
		help.add(About);
		help.addSeparator();
		help.add(Wiki);
		help.add(Updates);
		return fileBar;
	}
	public void actionPerformed(ActionEvent event)
	{
		if(event.getSource()==EditItem)
		{
			JOptionPane.showMessageDialog(null,"Hi");
		}
		if(event.getSource()==AddGateButton)
		{
			AddGate obj = new AddGate();
			obj.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			obj.setSize(500,300);
			obj.setLocation(300,200);
			obj.setVisible(true);
		}
		if(event.getSource()==Customise)
		{
			CustomiseToolbar object= new CustomiseToolbar();
			object.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			object.setSize(394,105);
			object.setLocation(200,100);
			object.setVisible(true);
		}
	}
	public void itemStateChanged(ItemEvent event) 
	{
		if(event.getSource()==EditItem)
		JOptionPane.showMessageDialog(null, "hi");
	}
	class OpenAction extends AbstractAction 
	{
        
        public OpenAction() 
        {
            super("Open");
            putValue(MNEMONIC_KEY, new Integer('O'));
        }
        
       
        public void actionPerformed(ActionEvent e) 
        {
        	statusbar.setText("Open");
            int retval = _fileChooser.showOpenDialog(WorkSpaceScreen.this);
            if (retval == JFileChooser.APPROVE_OPTION) 
            {
                File f = _fileChooser.getSelectedFile();
                try 
                {
                    FileReader reader = new FileReader(f);
               
                } 
                catch (IOException ioex) 
                {
                	JOptionPane.showMessageDialog(null, "An error occured");
                    System.out.println(e);
                    System.exit(1);
                }
            }
            statusbar.setText("Welcome to Circuit Pro");
        }
	}
   
    class SaveAction extends AbstractAction {
               
    	SaveAction() 
    	{
            super("Save");
            putValue(MNEMONIC_KEY, new Integer('S'));
        }
        
       
        public void actionPerformed(ActionEvent e)
        {
            int retval = _fileChooser.showSaveDialog(WorkSpaceScreen.this);
            if (retval == JFileChooser.APPROVE_OPTION) 
            {
                File f = _fileChooser.getSelectedFile();
                try 
                {
                    FileWriter writer = new FileWriter(f);
                }
                catch (IOException ioex)
                {
                    JOptionPane.showMessageDialog(WorkSpaceScreen.this, ioex);
                    System.exit(1);
                }
            }
        }
    }
    
   
    class ExitAction extends AbstractAction
    {
        
   
        public ExitAction() 
        {
            super("Exit");
            putValue(MNEMONIC_KEY, new Integer('X'));
        }
        
   
        public void actionPerformed(ActionEvent e) 
        {
        	dispose();
        }
    }
    
    class NewAction extends AbstractAction
    {
        
   
        public NewAction() 
        {
            super("New");
            putValue(MNEMONIC_KEY, new Integer('N'));
        }
        
   
        public void actionPerformed(ActionEvent e) 
        {
            
        }
    }
    
    class AboutAction extends AbstractAction
    {
        
    	private String str;
        public AboutAction() 
        {
            super("About");
            str="Circiut Pro  Project Created by Rajul Bhatnagar and Satyam Krishna";
            putValue(MNEMONIC_KEY, new Integer('A'));
        }
        
   
        public void actionPerformed(ActionEvent e) 
        {
            JOptionPane.showMessageDialog(null, str);
        }
    }
    
    class WikiAction extends AbstractAction
    {
        
   
        public WikiAction() 
        {
            super("Wiki");
            putValue(MNEMONIC_KEY, new Integer('W'));
        }
        
   
        public void actionPerformed(ActionEvent e) 
        {
            
        }
    }
    
    class UpdatesAction extends AbstractAction
    {
        
   
        public UpdatesAction() 
        {
            super("Update");
            putValue(MNEMONIC_KEY, new Integer('U'));
        }
        
   
        public void actionPerformed(ActionEvent e) 
        {
            
        }
    }


	
}
